package kkk;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import robocode.control.testing.RobotTestBed;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that Strafer always beats
 * SittingDuck.
 * 
 * Also illustrates the overriding of a set of methods from RobotTestBed to show how the testing
 * behavior can be customized and controlled.
 * 
 * @author Kyle Kakinami
 * 
 */
public class TestStraferVersusSpinBot extends RobotTestBed {

  /** True if the robot wins at least half the total rounds. */
  boolean wonHalf = false;

  /**
   * Specifies that SpinBot and Strafer are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SpinBot,kkk.Strafer";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * The actual test, which asserts that Strafer has won half the rounds against SpinBot.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    // Return the results in order of getRobotNames.
    BattleResults[] battleResults = event.getIndexedResults();
    // Sanity check that results[1] is Strafer (not strictly necessary, but illustrative).
    BattleResults straferResults = battleResults[1];
    String robotName = straferResults.getTeamLeaderName();
    assertEquals("Check that results[1] is Strafer", robotName, "kkk.Strafer*");

    if (straferResults.getFirsts() > (getNumRounds() / 5)) {
      wonHalf = true;
    }
    // Check to make sure Strafer won every round.
    assertTrue("Check Strafer winner", wonHalf);

  }
}
